package com.pl.dailycollection.controller;

import com.pl.dailycollection.dto.ResponseResult;
import com.pl.dailycollection.util.ParseExcel;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *
 * @Description: TODO
 * </p>
 * @ClassName UploadController
 * @Author pl
 * @Date 2020/4/15
 * @Version V1.0.0
 */
@RestController
public class UploadController {

    @PostMapping("/importAlumnis")
    @ResponseBody
    public ResponseResult<Void> importAlumnis(@RequestParam(value = "alumniData") MultipartFile file) throws IOException {
        InputStream inputStream=null;
        try {
            //输入流
            inputStream = file.getInputStream();
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            ParseExcel parser = new ParseExcel();
            //第三行开始读取
            int startRow = 2;

            List<String[]> result = parser.parseExcel(inputStream, suffix, startRow);
            int count=0;
            HashMap<String, String> originalMap = new HashMap<>();
            HashMap<String, String> unInsertMap = new HashMap<>();
            ArrayList<String> errorList = new ArrayList<>();
            int rolNum = 2;
            for(String[] ss : result){
                // 判断是否符合插入条件？进行插入逻辑：记录错误行
                if (vertify(originalMap,ss)){
                    //判断当前值在item表中是否存在？直插入nature表：1.插入item表，2.unInsertMap.put,3.insert nature表
                    if (!unInsertMap.containsKey(ss[1])){
                        //insert  item表
                        unInsertMap.put(ss[2],ss[3]);
                        //insert  nature表
                    }else {
                        //insert nature表
                    }
                }else {
                    errorList.add("第".concat(String.valueOf(rolNum).concat("行")));
                }
                rolNum++;
            }
            if(count==result.size()){
                //全部数据导入成功
                return new ResponseResult<Void>(ResponseResult.CodeStatus.OK, "用户已注销");
            }
            return new ResponseResult<Void>(ResponseResult.CodeStatus.OK, "用户已注销");

        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseResult<Void>(ResponseResult.CodeStatus.OK, "失败");
        } finally {
            //关闭流
            inputStream.close();
        }
    }


    public boolean vertify(HashMap<String, String> originalMap,String[] data){
        String ss = data[3];
        if (originalMap.containsKey(data[1]) && !StringUtils.isEmpty(data[2])&& !StringUtils.isEmpty(data[3])){
            return true;
        }

        return false;
    }

}